package com.huarui.company.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.ewaf.framework.model.ListModel;
import org.ewaf.framework.service.BaseService;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;

import com.huarui.companypackage.model.RoleEmployee;
import com.huarui.department.dao.IDepartmentDao;
import com.huarui.employee.dao.IEmployeeDao;
import com.huarui.payInvoice.util.PayInvoiceMessageEnum;
import com.huarui.permission.dao.PermissionDao;
import com.huarui.permission.service.PermissionService;
import com.huarui.roleEmployee.dao.IRoleEmployeeDao;
import com.huarui.user.model.SessionMessage;

public class CompanyPermissionService extends BaseService {
	private final static Logger logger = Logger
	.getLogger(CompanyPermissionService.class);
	private IDepartmentDao departmentDao;
	private IEmployeeDao employeeDao;
	private PermissionDao permissionDao;
	private IRoleEmployeeDao roleEmployeeDao;
	@SuppressWarnings("unchecked")
	public List comPerMissionTreeLoader(SessionMessage sm, String nodeId) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map> resultList = new ArrayList<Map>();
		if ("department".endsWith(nodeId)) {
			map.put("companyId", sm.getCompanyId());
			List<Map> list = departmentDao.getDepartmentsByConditions(map);
			for (Map emp : list) {
				Map retMap = new HashMap();
				retMap.put("id", emp.get("id"));
				retMap.put("text", emp.get("name"));
				resultList.add(retMap);
			}
		} else {
			Integer departmentId = Integer.valueOf(nodeId);
			map.put("departmentId", departmentId);
			List<Map> list = employeeDao.getListBycondition(map);
			for (Map emp : list) {
				Map retMap = new HashMap();
				retMap.put("id", emp.get("id"));
				retMap.put("text", emp.get("name"));
				retMap.put("leaf", true);
				resultList.add(retMap);
			}
		}
		return resultList;
	}

	@SuppressWarnings("unchecked")
	public List<Map> getRoleListByCondition(Map map) {
		List<Map> list = roleEmployeeDao.getRoleList(map);
		List<Map> resultList = new ArrayList<Map>();
		for (Map role : list) {
			Map retMap = new HashMap();
			retMap.put("id", role.get("id"));
			retMap.put("text", role.get("name"));
			retMap.put("leaf", true);
			resultList.add(retMap);
		}
		return resultList;
	}

	public List<Map> getEmployeeList(ListModel lm,Map map) {
		Integer total=employeeDao.getCountByCondition(map);
		lm.setTotal(total);
		map.put("listModel", lm);
		List<Map> list = employeeDao.getListBycondition(map);
		return list;
	}

	public Map<String, Object> saveCompanyPermission(final SessionMessage sm,final List<Map> roleList,final Integer pageRoleId) {
		final Map<String, Object> resultMap = new HashMap<String, Object>();
		if(sm==null||roleList==null||roleList.size()<1||pageRoleId==null){
			resultMap.put(PayInvoiceMessageEnum.SUCCESS.getMsg(),PayInvoiceMessageEnum.ERROR_MSG_DATABASE.getMsg());
			return resultMap;
		}
		try {
			transactionTemplate.execute(new TransactionCallback() {
				@SuppressWarnings("unchecked")
				@Override
				public Object doInTransaction(TransactionStatus status) {
					Map map=new HashMap();
					for (Map role : roleList) {
						map.clear();
						Integer employeeId = (Integer) role.get("id");
						map.put("employeeId", employeeId);
						Integer roleId = (Integer) role.get("roleId");
						map.put("roleId", pageRoleId);
						map.put("companyId", sm.getCompanyId());
						RoleEmployee roleEmployee=roleEmployeeDao.getRoleEmployee(map);
						if(roleId==null&&roleEmployee!=null){
							roleEmployeeDao.deleteRoleEmployee(map);
						}else if(roleId!=null&&roleEmployee==null){
							//权限有缺陷
							RoleEmployee roleE=new RoleEmployee();
							roleE.setRoleId(roleId);
							roleE.setEmployeeId(employeeId);
							roleEmployeeDao.createRoleEmployee(roleE);
						}
					}
					return null;
				}
			});
		} catch (Exception e) {
			logger.error("更新角色员工表出错", e);
			resultMap.put(PayInvoiceMessageEnum.SUCCESS.getMsg(),
					PayInvoiceMessageEnum.ERROR_MSG_DATABASE.getMsg());
			return resultMap;
		}
		resultMapInfo(resultMap);
		return resultMap;
	}
	private void resultMapInfo(Map<String,Object> resultMap){
		if(!resultMap.containsKey(PayInvoiceMessageEnum.SUCCESS.getMsg())){
			resultMap.put(PayInvoiceMessageEnum.SUCCESS.getMsg(),PayInvoiceMessageEnum.RESULT_TYPE_SUCCESS.getMsg());
		}
	}
	public void setDepartmentDao(IDepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}

	public void setEmployeeDao(IEmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}

	public void setPermissionDao(PermissionDao permissionDao) {
		this.permissionDao = permissionDao;
	}



	public void setRoleEmployeeDao(IRoleEmployeeDao roleEmployeeDao) {
		this.roleEmployeeDao = roleEmployeeDao;
	}
	
}
